{extend name="index/nav" /}}
{block name="main"}
<style>
    .imgar{
        overflow: hidden;
    }
    .imgar>span,.imgar>input{
        float: left;
    }
    #mytable td{
        line-height: 100px;;
    }
</style>
<div class="body">
    <div class="">
        <h2>资讯图片</h2>
    </div>
    <form action="{:url('admin/index/addBanner')}" enctype="multipart/form-data" method="post">
        <div class="imgar">
            <span>添加图片: </span>
            <input type="hidden" id="toImageAdd" name="id">
            <input type="file" id="doc-ipt-1"name="image">
            <input type="submit" value="提交"/>
        </div>
    </form>
    <div class="index-table news_table">
        <table class="am-table am-table-bordered am-table-radius am-table-striped" id="mytable">
            <thead>
            <tr>
                <th>banner_id</th>
                <th>图片</th>
                <th colspan="2">操作</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>


<script src='/static/index/js/jquery-1.11.0.min.js'></script>
<script>
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    $(function() {
        $('#collapse-nav3').addClass('am-in');
        //       屏幕高度
        var heigth = document.body.offsetHeight - 90;
        $('.body').css('height', heigth + 'px');


//      表格数据
        var arr=document.cookie.split(';');
        var id=arr[arr.length-1];
        $.post('{:url("admin/index/selBanner")}',{id:id}, function (res) {
            res=JSON.parse(res);
            console.log(res);
            $('#toImageAdd').val(id);
            for(var i=0;i<res.length;i++){
                var data=res[i];
                if(data.status ==0){
                    $('#mytable tbody').append(`<tr>
                    <td>${data.banner_id}</td>
                     <td><img src=`+data.image+` height='100px'></td>
                     <td><a href='javascript:void(0)' class='conter' data_id=${data.banner_id}>设置成封面</a></td>
                      <td><a href='javascript:void(0)' class='image_delect' data_id=${data.banner_id} data_img=${data.image}>删除</a></td>
                    </tr>`);
                }else{
                    $('#mytable tbody').append(`<tr>
                    <td>${data.banner_id}</td>
                    <td><img src=`+data.image+` height='100px'></td>
                    <td >封面</td>
                    <td ><a href='javascript:void(0)' class='image_delect' data_id=${data.banner_id} data_img=${data.image}>删除</a></td>
                    </tr>`);
                }
            }
        })
    });

    //  设置成封面
    $('.news_table').delegate('.conter','click',function(){
        var id=$(this).attr('data_id');
        var news_id=$(this).attr('data1_id');
        $.post('{:url("admin/index/bannerImg")}',{id:id},function (res) {
//            console.log(res);
            window.location.reload();
        })
    })

    //  删除
    $('.news_table').delegate('.image_delect','click',function(){
        var id=$(this).attr('data_id');
        var img=$(this).attr('data_img');
//        console.log(img)
        $.post('{:url("admin/index/delBanner")}',{id:id,img:img},function (res) {
           location.reload();
        })
    })
</script>
{/block}